package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Client;
import com.scau.model.business.OrderList;
import com.scau.model.business.OrderShoe;
import com.scau.model.view.OrderView;
import com.scau.service.impl.business.ClientService;
import com.scau.service.impl.business.OrderListService;
import com.scau.service.impl.business.OrderShoeService;
import com.scau.service.impl.view.OrderViewService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class OrderListAction extends BaseAction implements Serializable,
		ModelDriven<OrderList> {
	/**
	 * serialVersionUID:TODO（用一句话描述这个变量表示什么）
	 * 
	 * @since Ver 1.1
	 */

	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager;
	private OrderListService orderListService;
	private OrderList order;
	private ClientService clientService;
	private OrderViewService orderViewService;
	private OrderShoeService orderShoeService;

	public String list() {
		// 显示列表
		int totalRows = orderViewService.getRecordCount(new OrderView());
		String URL =getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<OrderView> resourceList = orderViewService.list(new OrderView(),
				this.pager.getPageStartRow(), pager.getPageSize(), null, null);
		pager.setData(resourceList);

		return "list";
	}
	
	public String get() {
		order = orderListService.get(order);
		List<Client> clientList = clientService.listAll(new Client());
		request.setAttribute("clientList", clientList);
		return "edit";
	}

	public String save() {
		try {
			orderListService.save(order);			
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("order", order);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			if (null != id && !("".equals(id))) {
//				OrderShoe orderShoe = new OrderShoe();
//				orderShoe.setOrder_id(Long.parseLong(id));
//				orderShoeService.delete(orderShoe);
				order.setId(Long.parseLong(id));
				orderListService.delete(order);
			}
		}
		return "list";
	}

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public OrderList getOrder() {
		return this.order;
	}

	public void setOrder(OrderList order) {
		this.order = order;
	}

	public OrderListService getOrderListService() {
		return orderListService;
	}

	public ClientService getClientService() {
		return clientService;
	}

	public OrderViewService getOrderViewService() {
		return orderViewService;
	}

	public OrderShoeService getOrderShoeService() {
		return orderShoeService;
	}

	@Resource
	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	@Resource
	public void setOrderViewService(OrderViewService orderViewService) {
		this.orderViewService = orderViewService;
	}

	@Resource
	public void setOrderShoeService(OrderShoeService orderShoeService) {
		this.orderShoeService = orderShoeService;
	}

	@Resource
	public void setOrderListService(OrderListService orderListService) {
		this.orderListService = orderListService;
	}

	@Override
	public OrderList getModel() {
		// TODO Auto-generated method stub
		return order;
	}

}
